package org.noote.libs.virtualpad;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

public class VirtualTouchPad_Renderer {

	Paint _paintPad, _paint1PadFont, _paintVSFont;
	
	Bitmap _bitmapVPad_Analogic;
	Bitmap _bitmapVPad_Stick;
	Bitmap _bitmapVPad_StickPressed;
	Bitmap _bitmapVPad_5D;	
	Bitmap _bitmapVPad_5D_UpPressed;
	Bitmap _bitmapVPad_5D_DownPressed;
	Bitmap _bitmapVPad_5D_CenterPressed;
	Bitmap _bitmapVPad_5D_LeftPressed;
	Bitmap _bitmapVPad_5D_RightPressed;
	Bitmap _bitmapVPad_1D;
	Bitmap _bitmapVPad_1D_Pressed;
	Bitmap _bitmapVPad_1D_Locked;
	Bitmap _bitmapVPad_VerticalSlider;
	Bitmap _bitmapVPad_VerticalSlider_Cursor;
	Bitmap _bitmapVPad_VerticalSlider_CursorPressed;
	
	public VirtualTouchPad_Renderer()
	{
		_paintPad = new Paint();
		_paintPad.setColor(Color.BLACK);
		_paintPad.setStyle(Paint.Style.STROKE);

		_paint1PadFont = new Paint();
        _paint1PadFont.setColor(Color.BLACK);
        _paint1PadFont.setStyle(Paint.Style.STROKE);
        _paint1PadFont.setTextSize(22);
        _paint1PadFont.setShadowLayer(2, 0, 0, Color.GRAY);
        
        _paintVSFont = new Paint();
        _paintVSFont.setColor(Color.BLACK);
        _paintVSFont.setStyle(Paint.Style.STROKE);
        _paintVSFont.setTextSize(14);
        _paintVSFont.setShadowLayer(2, 0, 0, Color.GRAY);
	}
	public void loadImages(Context ctx, float fScale)
	{
		loadImagesForA(ctx, fScale);
		loadImagesFor1D(ctx, fScale);
		loadImagesFor5D(ctx, fScale);
		loadImagesForVS(ctx, fScale);
		
		_paint1PadFont.setTextSize(22*fScale);
		_paintVSFont.setTextSize(14*fScale);
	}
	
	public void loadImagesForA(Context ctx, float fScale)
	{
		_bitmapVPad_Analogic = _load_image_from_resource(ctx, R.drawable.vpad_analogic_transparent, fScale);
		_bitmapVPad_Stick = _load_image_from_resource(ctx, R.drawable.vpad_stick_transparent, fScale);
		_bitmapVPad_StickPressed = _load_image_from_resource(ctx, R.drawable.vpad_stick_pressed_transparent, fScale);
	}
	public void loadImagesFor1D(Context ctx, float fScale)
	{
       	_bitmapVPad_1D = _load_image_from_resource(ctx, R.drawable.vpad_1pad_transparent, fScale);
       	_bitmapVPad_1D_Pressed = _load_image_from_resource(ctx, R.drawable.vpad_1pad_pressed_transparent, fScale);
       	_bitmapVPad_1D_Locked = _load_image_from_resource(ctx, R.drawable.vpad_1pad_locked_transparent, fScale);
	}
	public void loadImagesFor5D(Context ctx, float fScale)
	{
		_bitmapVPad_5D = _load_image_from_resource(ctx, R.drawable.vpad_5pads_transparent, fScale);
		_bitmapVPad_5D_UpPressed = _load_image_from_resource(ctx, R.drawable.vpad_5pads_up_pressed_transparent, fScale);
       	_bitmapVPad_5D_DownPressed = _load_image_from_resource(ctx, R.drawable.vpad_5pads_down_pressed_transparent, fScale);
       	_bitmapVPad_5D_CenterPressed = _load_image_from_resource(ctx, R.drawable.vpad_5pads_center_pressed_transparent, fScale);
       	_bitmapVPad_5D_LeftPressed = _load_image_from_resource(ctx, R.drawable.vpad_5pads_left_pressed_transparent, fScale);
       	_bitmapVPad_5D_RightPressed = _load_image_from_resource(ctx, R.drawable.vpad_5pads_right_pressed_transparent, fScale);
	}
	public void loadImagesForVS(Context ctx, float fScale)
	{
		_bitmapVPad_VerticalSlider = _load_image_from_resource(ctx, R.drawable.vpad_verticalslider_transparent, fScale);
		_bitmapVPad_VerticalSlider_Cursor = _load_image_from_resource(ctx, R.drawable.vpad_verticalslider_cursor_transparent, fScale);
		_bitmapVPad_VerticalSlider_CursorPressed = _load_image_from_resource(ctx, R.drawable.vpad_verticalslider_cursor_pressed_transparent, fScale);
	}

	public int getWidth(VirtualTouchPad pad)
	{
		if(_bitmapVPad_5D != null)
			return _bitmapVPad_5D.getWidth();
		return 0;
	}
	public int getHeight(VirtualTouchPad pad)
	{
		if(_bitmapVPad_5D != null)
			return _bitmapVPad_5D.getHeight();
		return 0;
	}
	public int getWidth(Virtual1DPad pad)
	{
		if(_bitmapVPad_1D != null)
			return _bitmapVPad_1D.getWidth();
		return 0;
	}
	public int getHeight(Virtual1DPad pad)
	{
		if(_bitmapVPad_1D != null)
			return _bitmapVPad_1D.getHeight();
		return 0;
	}
	public int getWidth(VirtualVerticalSlider pad)
	{
		if(_bitmapVPad_VerticalSlider != null)
			return _bitmapVPad_VerticalSlider.getWidth();
		return 0;
	}
	public int getHeight(VirtualVerticalSlider pad)
	{
		if(_bitmapVPad_VerticalSlider != null)
			return _bitmapVPad_VerticalSlider.getHeight();
		return 0;
	}
	public void draw(Canvas canvas, VirtualPad_Input inputs)
	{
		for(VirtualTouchPad pad : inputs)
		{
			if(pad instanceof Virtual1DPad)
				draw(canvas, (Virtual1DPad)pad);
			else if(pad instanceof Virtual5DPad)
				draw(canvas, (Virtual5DPad)pad);
			else if(pad instanceof Virtual9DPad)
				draw(canvas, (Virtual9DPad)pad);
			else if(pad instanceof VirtualVerticalSlider)
				draw(canvas, (VirtualVerticalSlider)pad);
			else
				draw(canvas, pad);
		}
	}
	public void draw(Canvas canvas, VirtualTouchPad pad)
	{
		if(_bitmapVPad_Analogic != null)
			canvas.drawBitmap(_bitmapVPad_Analogic, pad._iMin_X, pad._iMin_Y, _paintPad);
		
		int iSizeX = pad._iMax_X - pad._iMin_X;
		int iSizeY = pad._iMax_Y - pad._iMin_Y;
		int iCenterX = pad._iMin_X+iSizeX/2;
		int iCenterY = pad._iMin_Y+iSizeY/2;
		int tCenterStickX = _bitmapVPad_StickPressed.getWidth()/2;
		int iCenterStickY = _bitmapVPad_StickPressed.getHeight()/2;
		int iPosX = (int) (iCenterX-iSizeX*pad.getX()/2-tCenterStickX);
		int iPosY = (int) (iCenterY+iSizeY*pad.getY()/2-iCenterStickY);

		if(_bitmapVPad_StickPressed != null && pad.getTouch()>=0.5f)
		{
			canvas.drawBitmap(_bitmapVPad_StickPressed, iPosX, iPosY, _paintPad);
		}
		else if(_bitmapVPad_Stick != null && pad.getTouch()<0.5)
		{
			canvas.drawBitmap(_bitmapVPad_Stick, iPosX, iPosY, _paintPad);
		}
	}
	public void draw(Canvas canvas, Virtual1DPad pad)
	{
		draw(canvas, pad, pad._sText);
	}
	public void draw(Canvas canvas, Virtual1DPad pad, String text)
	{
		if(_bitmapVPad_1D != null)
			canvas.drawBitmap(_bitmapVPad_1D, pad._iMin_X, pad._iMin_Y, _paintPad);

		if(_bitmapVPad_1D_Pressed != null && pad.getButton())
			canvas.drawBitmap(_bitmapVPad_1D_Pressed, pad._iMin_X, pad._iMin_Y, _paintPad);
		else if(_bitmapVPad_1D_Locked != null && pad.isLocked())
			canvas.drawBitmap(_bitmapVPad_1D_Locked, pad._iMin_X, pad._iMin_Y, _paintPad);

		if(text != null && text.length()>0)
		{
			Rect bounds = new Rect();
			_paint1PadFont.getTextBounds(text, 0, text.length(), bounds);
			
			int iCenterX = pad._iMin_X+(pad._iMax_X - pad._iMin_X)/2;
			int iCenterY = pad._iMin_Y+(pad._iMax_Y - pad._iMin_Y)/2;
			
			canvas.drawText(text, iCenterX-bounds.centerX(), iCenterY-bounds.centerY(), _paint1PadFont);
		}
	}
	public void draw(Canvas canvas, Virtual5DPad pad)
	{
		if(_bitmapVPad_5D != null)
			canvas.drawBitmap(_bitmapVPad_5D, pad._iMin_X, pad._iMin_Y, _paintPad);
		
		if(_bitmapVPad_5D_CenterPressed != null && pad.getButtonCenter())
			canvas.drawBitmap(_bitmapVPad_5D_CenterPressed,pad._iMin_X, pad._iMin_Y, _paintPad);
		
		if(_bitmapVPad_5D_UpPressed != null && pad.getButtonUp())
			canvas.drawBitmap(_bitmapVPad_5D_UpPressed, pad._iMin_X, pad._iMin_Y, _paintPad);
		if(_bitmapVPad_5D_DownPressed != null && pad.getButtonDown())
			canvas.drawBitmap(_bitmapVPad_5D_DownPressed, pad._iMin_X, pad._iMin_Y, _paintPad);
		if(_bitmapVPad_5D_LeftPressed != null && pad.getButtonLeft())
			canvas.drawBitmap(_bitmapVPad_5D_LeftPressed, pad._iMin_X, pad._iMin_Y, _paintPad);
		if(_bitmapVPad_5D_RightPressed != null && pad.getButtonRight())
			canvas.drawBitmap(_bitmapVPad_5D_RightPressed, pad._iMin_X, pad._iMin_Y, _paintPad);
	}
	public void draw(Canvas canvas, Virtual9DPad pad)
	{
		if(_bitmapVPad_5D != null)
			canvas.drawBitmap(_bitmapVPad_5D, pad._iMin_X, pad._iMin_Y, _paintPad);
		
		if(_bitmapVPad_5D_CenterPressed != null && pad.getButtonCenter())
			canvas.drawBitmap(_bitmapVPad_5D_CenterPressed,pad._iMin_X, pad._iMin_Y, _paintPad);
		
		if(_bitmapVPad_5D_UpPressed != null && pad.getButtonUp())
			canvas.drawBitmap(_bitmapVPad_5D_UpPressed, pad._iMin_X, pad._iMin_Y, _paintPad);
		if(_bitmapVPad_5D_DownPressed != null && pad.getButtonDown())
			canvas.drawBitmap(_bitmapVPad_5D_DownPressed, pad._iMin_X, pad._iMin_Y, _paintPad);
		if(_bitmapVPad_5D_LeftPressed != null && pad.getButtonLeft())
			canvas.drawBitmap(_bitmapVPad_5D_LeftPressed, pad._iMin_X, pad._iMin_Y, _paintPad);
		if(_bitmapVPad_5D_RightPressed != null && pad.getButtonRight())
			canvas.drawBitmap(_bitmapVPad_5D_RightPressed, pad._iMin_X, pad._iMin_Y, _paintPad);
		
		if(_bitmapVPad_5D_UpPressed != null && _bitmapVPad_5D_LeftPressed != null && pad.getButtonUpLeft())
		{
			canvas.drawBitmap(_bitmapVPad_5D_UpPressed, pad._iMin_X, pad._iMin_Y, _paintPad);
			canvas.drawBitmap(_bitmapVPad_5D_LeftPressed, pad._iMin_X, pad._iMin_Y, _paintPad);
		}
		if(_bitmapVPad_5D_UpPressed != null && _bitmapVPad_5D_RightPressed != null && pad.getButtonUpRight())
		{
			canvas.drawBitmap(_bitmapVPad_5D_UpPressed, pad._iMin_X, pad._iMin_Y, _paintPad);
			canvas.drawBitmap(_bitmapVPad_5D_RightPressed, pad._iMin_X, pad._iMin_Y, _paintPad);
		}
		if(_bitmapVPad_5D_DownPressed != null && _bitmapVPad_5D_LeftPressed != null && pad.getButtonDownLeft())
		{
			canvas.drawBitmap(_bitmapVPad_5D_DownPressed, pad._iMin_X, pad._iMin_Y, _paintPad);
			canvas.drawBitmap(_bitmapVPad_5D_LeftPressed, pad._iMin_X, pad._iMin_Y, _paintPad);
		}
		if(_bitmapVPad_5D_DownPressed != null && _bitmapVPad_5D_RightPressed != null && pad.getButtonDownRight())
		{
			canvas.drawBitmap(_bitmapVPad_5D_DownPressed, pad._iMin_X, pad._iMin_Y, _paintPad);
			canvas.drawBitmap(_bitmapVPad_5D_RightPressed, pad._iMin_X, pad._iMin_Y, _paintPad);
		}
	}
	public void draw(Canvas canvas, VirtualVerticalSlider pad)
	{
		draw(canvas, pad, pad._sText);
	}
	public void draw(Canvas canvas, VirtualVerticalSlider pad, String text)
	{
		if(_bitmapVPad_VerticalSlider != null)
			canvas.drawBitmap(_bitmapVPad_VerticalSlider, pad._iMin_X, pad._iMin_Y, _paintPad);
		
		float fCursor = pad.getPosition();
		
		int iCursorPositionX =  pad._iMin_X;
		int iCursorPositionY =  (int) (pad._iMin_Y+fCursor*(pad._iMax_Y - pad._iMin_Y)-(pad._iMax_X - pad._iMin_X)/2);
		
		if(_bitmapVPad_VerticalSlider_Cursor != null && !pad.getPressed())
			canvas.drawBitmap(_bitmapVPad_VerticalSlider_Cursor, iCursorPositionX, iCursorPositionY, _paintPad);
		else if(_bitmapVPad_VerticalSlider_CursorPressed != null && pad.getPressed())
			canvas.drawBitmap(_bitmapVPad_VerticalSlider_CursorPressed, iCursorPositionX, iCursorPositionY, _paintPad);
		if(text.length()>0  && text.length()>0)
		{
			Rect bounds = new Rect();
			_paintVSFont.getTextBounds(text, 0, text.length(), bounds);
			
			canvas.drawText(text, iCursorPositionX+_bitmapVPad_VerticalSlider.getWidth()/2-bounds.centerX(), iCursorPositionY+_bitmapVPad_VerticalSlider.getWidth()/2-bounds.centerY(), _paintVSFont);
		}
	}

	private Bitmap _load_image_from_resource(Context ctx, int idRessource, float fScale)
	{
		final Bitmap loadedBitmap = BitmapFactory.decodeStream(ctx.getResources().openRawResource(idRessource));
		if(loadedBitmap != null)
			return Bitmap.createScaledBitmap(loadedBitmap, (int)(loadedBitmap.getWidth()*fScale), (int)(loadedBitmap.getHeight()*fScale), true);
		
		return null;
	}
}
